package com.qf.Dao;

import com.qf.Enity.User;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
public class UserDaoImpl implements UserDao{
    @Override
    public User login(String username, String password) {

        Connection conn = null;
        PreparedStatement ps = null;
        User user = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/java2405?useSSL=false", "root", "123456");
            ps = conn.prepareStatement("select * from tb_user where username = ? and password = ?");
            ps.setString(1,username);
            ps.setString(2,password);
            rs = ps.executeQuery( );
            if (rs.next()) {
                user = new User(  );
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
            }
            System.out.println(rs);


        }catch (Exception e){
            System.out.println("登录bug" );
            e.printStackTrace();
        }finally {
            try{
                rs.close();
                ps.close();
                conn.close();
            }catch (Exception e) {
                System.out.println("关闭连接出问题" );
                e.printStackTrace();
            }
        }

        return user;
    }

    @Override
    public boolean regist(User user) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/java2405?useSSL=false", "root", "123456");
            ps = conn.prepareStatement("insert into tb_user (username,password,phone,sex) values (?,?,?,?)");
            ps.setString(1,user.getUsername());
            ps.setString(2,user.getPassword());
            ps.setString(3,user.getPhone());
            ps.setString(4,user.getSex());
            int i = ps.executeUpdate();
            return i > 0 ? true:false;

        }catch (Exception e){
            System.out.println("登录bug" );
            e.printStackTrace();
        }finally {
            try{
                rs.close();
                ps.close();
                conn.close();
            }catch (Exception e) {
                System.out.println("关闭连接出问题" );
                e.printStackTrace();
            }
        }

        return false;

    }
}

